package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.constans.CapitalstructurePref;
import com.simon.annual.constans.CapitalstructureType;
import com.simon.annual.entity.ReportCapitalstructure;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportOrdinaryshareholder;
import com.simon.annual.repositoy.IReportCapitalstructureRepositoy;
import com.simon.annual.repositoy.IReportNeeqRepositoy;
import com.simon.annual.repositoy.IReportOrdinaryshareholderRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IEquityShareholderInfo;
/**
 * 第六节股本变动及股东情况
 * @author simon
 * */
@Service
public class FindEquityShareholderInfoImpl implements IEquityShareholderInfo{
	
	@Resource
	private ICompanyPublicInfo companyPublicInfo;
	@Resource
	private IReportOrdinaryshareholderRepositoy reportOrdinaryshareholderRepositoy;
	@Resource
	private IReportCapitalstructureRepositoy reportCapitalstructureRepositoy;
	@Resource
	private IReportNeeqRepositoy reportNeeqRepositoy;
	@Resource
	private Environment env;
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	
	@Override
	public ReportNeeq analysis(Document doc, ReportNeeq reportNeeq, int companyPtrId) {
		//普通股股本结构
		Element capital = doc.getElementsMatchingOwnText(env.getProperty("pdf.six.capital.info")).last();
		Element capitalValue = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.six.structure"), capital, 0);
		Element  capitalstructure = companyPublicInfo.findNextTagNoMer(capitalValue, capitalValue);
		Elements reportCapitalstructureTr = capitalstructure.select("table tr");
		List<ReportCapitalstructure> reportCapitalstructureList = new ArrayList<ReportCapitalstructure>();
		//普通股股本结构
		CapitalstructureType[] values =CapitalstructureType.values();
		for (int i = 0; i < values.length; i++) {
			Elements reportCapitalstructureTd = reportCapitalstructureTr.get(i+2).children();
			ReportCapitalstructure reportCapitalstructure= new ReportCapitalstructure();
			reportCapitalstructure.setType(values[i].getCode());
			reportCapitalstructure.setBgnShares(reportCapitalstructureTd.get(2).text());
			reportCapitalstructure.setEndShares(reportCapitalstructureTd.get(5).text());
			reportCapitalstructure.setProjectScopeId(companyPtrId);
			reportCapitalstructureList.add(reportCapitalstructure);
		}
		//总股本
		Elements tr2 = reportCapitalstructureTr.get(10).children();
		ReportCapitalstructure reportTr2= new ReportCapitalstructure();
		reportTr2.setType("Capitalization");
		reportTr2.setBgnShares(tr2.get(1).text());
		reportTr2.setEndShares(tr2.get(4).text());
		reportTr2.setProjectScopeId(companyPtrId);
		reportCapitalstructureList.add(reportTr2);
		
		reportNeeq.setOrdinaryShareholdersNo(capitalstructure.select("table tr > *").last().text());
		//普通股前十名股东情况
		Element ordinaryShareholder = companyPublicInfo.findNextTag("table", capitalstructure);
		Elements ordinarytr = ordinaryShareholder.select("table tr");
		List<ReportOrdinaryshareholder> reportOrdinaryshareholderList = new ArrayList<ReportOrdinaryshareholder>();
		for (int i = 1; i < ordinarytr.size(); i++) {
			Elements td = ordinarytr.get(i).children();
			if(td.size()==ordinarytr.get(i-1).children().size()){
				ReportOrdinaryshareholder reportOrdinaryshareholder = new ReportOrdinaryshareholder();
				reportOrdinaryshareholder.setRank(td.get(0).text());
				reportOrdinaryshareholder.setName(td.get(1).text());
				reportOrdinaryshareholder.setBgnShares(td.get(2).text());
				reportOrdinaryshareholder.setEndShares(td.get(3).text());
				reportOrdinaryshareholder.setEndLimitedShares(td.get(4).text());
				reportOrdinaryshareholder.setEndUnlimitedShares(td.get(5).text());
				reportOrdinaryshareholder.setProjectScopeId(companyPtrId);
				reportOrdinaryshareholderList.add(reportOrdinaryshareholder);
			}else{
				break;
			}
		}
		//前十名股东间相互关系说明
		reportNeeq.setTopShareholderRelationship(ordinarytr.last().text());
		//优先股股本基本情况
		Element pref = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.six.pref"), ordinaryShareholder, -3);
		if(pref!=null){
			Element prefTable = companyPublicInfo.getNextTag(pref, pref);
			Elements prefTr = prefTable.select("table tr");
			if(prefTr.size()>2){
				CapitalstructurePref[] prefs =CapitalstructurePref.values();
				for (int i = 0; i < prefs.length; i++) {
					Elements prefTds = prefTr.get(i+1).children();
					ReportCapitalstructure reportCapitalstructure= new ReportCapitalstructure();
					reportCapitalstructure.setType(values[i].getCode());
					reportCapitalstructure.setBgnShares(prefTds.get(1).text());
					reportCapitalstructure.setEndShares(prefTds.get(3).text());
					reportCapitalstructure.setProjectScopeId(companyPtrId);
					reportCapitalstructureList.add(reportCapitalstructure);
				}
			}
		}
		reportCapitalstructureRepositoy.save(reportCapitalstructureList);
		//控股股东、实际控制人情况
		Element controlling = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.six.contr"), ordinaryShareholder, 0);
		if(controlling!=null){
			Element controllingValue = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.six.contr.end"), controlling, "",0);
			reportNeeq.setControllingStakeInfo(controllingValue.text());
		}
		//实际控制人情况
		Element actualController = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.six.actualContr"), ordinaryShareholder, -5);
		if(actualController!=null){
			Element actualControllerValue = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.six.actualContr.end"), actualController, "",0);
			reportNeeq.setControllingStakeInfo(actualControllerValue.text());
		}
		//股份代持情况
		//TODO:...
		return reportNeeqRepositoy.save(reportNeeq);
	}

}
